package me.jzn.alib.utils;

import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;

import androidx.annotation.Nullable;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 与SysIntentUtil不同，该类是用来跳转的，一般不判断是否安装，只判断跳转是否成功.
 * 一般带有参数。
 * 建议直接用@see CommUriUtil
 */
@Deprecated
public class SysJumpUtil {
    private static final Logger log = LoggerFactory.getLogger(SysJumpUtil.class);

    public static boolean jump2Qq(Activity context, String qq) {
        try {
            String url = CommUriUtil.buildQQUri(qq);
            context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
            return true;
        } catch (Throwable e) {
            log.warn("跳转qq错误,可能没有安装QQ:", e);
            return false;
        }
    }

    public static boolean jump2QqGroup(Activity context, String key) {
        Intent intent = new Intent();
        intent.setData(Uri.parse(CommUriUtil.buildQQGroupUri(key)));
        // 此Flag可根据具体产品需要自定义，如设置，则在加群界面按返回，返回手Q主界面，不设置，按返回会返回到呼起产品界面    //intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        try {
            context.startActivity(intent);
            return true;
        } catch (Exception e) {
            // 未安装手Q或安装的版本不支持
            log.warn("跳转qq群错误,可能没有安装QQ:", e);
            return false;
        }
    }

    public static boolean jumpToMarket(Activity context, String packageName) {
        Uri uri = Uri.parse(CommUriUtil.buildMarketUri(packageName));
        Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri);
        try {
            context.startActivity(goToMarket);
            return true;
        } catch (ActivityNotFoundException e) {
            return false;
        }
    }

    /**
     * @param appId     可以位null，默认位20000067,nebula官方默认。
     * @param qrDecoded 来自商家的收款码（二维码）解析
     */
    public static boolean jumpToAliPay(Activity context, @Nullable String appId, String qrDecoded) {
        String url = CommUriUtil.buildAliPayUri(appId, qrDecoded);
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));//Intent.parseUri(url, Intent.URI_INTENT_SCHEME);
        try {
            context.startActivity(intent);
            return true;
        } catch (Throwable e) {
            log.warn("跳转支付宝错误,可能没有安装支付宝:", e);
            return false;
        }
    }
}
